package com.atguigu.cms.controller;


import com.atguigu.cms.entity.CrmBanner;
import com.atguigu.cms.service.CrmBannerService;
import com.atguigu.commonUtils.R;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台Banner管理接口
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/cms/bannerAdmin")
//@CrossOrigin //解决跨域问题
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询banner轮播图
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页查询Banner")
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit) {

        Page<CrmBanner> bannerPage = new Page(page, limit);
        bannerService.page(bannerPage, null);

        return R.ok().data("items", bannerPage.getRecords()).data("total", bannerPage.getTotal());
    }

    /**
     * 添加banner
     *
     * @param crmBanner
     * @return
     */
    @ApiOperation("添加Banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok();
    }

    /**
     * 根据ID查询Banner
     *
     * @param bannerId
     * @return
     */
    @ApiOperation("查询Banner")
    @GetMapping("getBannerById/{bannerId}")
    public R getBannerById(@PathVariable String bannerId) {
        CrmBanner banner = bannerService.getById(bannerId);
        return R.ok().data("item", banner);
    }

    /**
     * 修改banner
     *
     * @param banner
     * @return
     */
    @ApiOperation("修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 删除banner
     *
     * @param bannerId
     * @return
     */
    @ApiOperation("删除Banner")
    @DeleteMapping("removeById/{bannerId}")
    public R removeById(@PathVariable String bannerId) {
        bannerService.removeById(bannerId);
        return R.ok();
    }
}

